/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mx.itschapala.sdw2.vista;

import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;
import mx.itschapala.dsw2.biblioteca8a.bl.CategoriablLocal;
import mx.itschapala.dsw2.biblioteca8a.modelo.Categoria;

/**
 *
 * @author Maricela
 */
@ManagedBean
@SessionScoped
public class CategoryBean {
   
//---------- EJB--------------------
     @EJB
    private CategoriablLocal categoriabl;
//---------- Variables---------------
    private List<Categoria> lista;
    private Categoria category;
    private String accion;
//---------- Constructor------------
    public CategoryBean() {
        category = new Categoria ();
    }
///--------- Metodos Get y Set-------
     public List<Categoria> getLista() {
        lista=categoriabl.getAll();
        return lista;
    }
     public Categoria getCategory() {
        return category;
    }

    public void setCategory(Categoria category) {
        this.category = category;
    }
//---------- Acciones----------------
    public String procesarAccion (){
    if (accion.equalsIgnoreCase("Nuevo")){
     categoriabl.register(category);
    }else {
        if (accion.equalsIgnoreCase("Editar")){
    categoriabl.modify(category);
    }else if (accion.equalsIgnoreCase("Eliminar")){
    categoriabl.delete(category);
    }
    }
    accion ="";
    return "Categoria";
    
    }
//---------- Listeners---------------
     public void prepararNuevo (ActionEvent evt){
    category= new Categoria ();
    accion="Nuevo";
    }
    public void prepararEditar (ActionEvent evt){
//        int index=Integer.parseInt(evt.getComponent().getClientId().split(":")[2]);
//        System.out.println(index);
//        category = lista.get(index);
        accion = "Editar";
    }
    
    public void prepararEliminar (ActionEvent evt){
//        int index=Integer.parseInt(evt.getComponent().getClientId().split(":")[2]);
//        System.out.println(index);
//        category = lista.get(index);
        accion = "Eliminar";
    }    
}
